<?php
class Subcategorymanager_Model_Pagedecision implements Subcategorymanager_Model_Mainable {
    
    private $action_handler;
    
    private $get_statuses_list;
    private $get_access_levels_list;
    private $get_category_lists;
    //private $get_module_lists;
    private $get_images_list;
    private $core_controller_csrftoken;
    
    private $subcategorymanager_data_getcurrentsubcategory;
    
    public function main(array $variables = null) {
        switch($variables['get']['page']){
            case 'dodaj-podkategorie':
                return $this->add_new_subcategory($variables);
                break;
            case 'wstaw-podkategorie':
                return $this->insert_new_subcategory($variables);
                break;
            case 'edytuj-podkategorie':
                return $this->edit_current_subcategory($variables);
                break;
            case 'aktualizuj-podkategorie':
                //var_dump($variables);
                //die();
                return $this->update_current_subcategory($variables);
                break;
            case 'usun-podkategorie':
                return $this->delete_current_subcategory($variables);
                break;
            default:
                return $this->default_action($variables);
                break;
        }
    }
    
    private function delete_current_subcategory(array $variables = null){
        $this->action_handler = new Subcategorymanager_Model_Deletesubcategorydata;
        return $this->action_handler->main($variables);
    }
    
    private function update_current_subcategory(array $variables = null){
        //var_dump($variables);
        //die();
        $this->action_handler = new Subcategorymanager_Model_Updatesubcategorydata;
        return $this->action_handler->main($variables);
    }
    
    private function edit_current_subcategory(array $variables = null){
        if(isset($variables['get']['name'])){
            $page = $this->get_other_data($variables);
            $page['template'] = WEB_ROOT_FOLDER."subcategorymanager/views/add.php";
            $this->subcategorymanager_data_getcurrentsubcategory = new Subcategorymanager_Data_Getcurrentsubcategory;
            $page['content'] = $this->subcategorymanager_data_getcurrentsubcategory->main($variables);
            return $page;
        }
        else {
            return $this->default_action($variables);
        }
    }
    
    private function insert_new_subcategory(array $variables = null){
        $this->action_handler = new Subcategorymanager_Model_Insertsubcategorydata;
        return $this->action_handler->main($variables);
    }
    
    private function add_new_subcategory(array $variables = null){
        $page = $this->get_other_data($variables);
        $page['template'] = WEB_ROOT_FOLDER."subcategorymanager/views/add.php";
        return $page;
    }
    
    private function default_action(array $variables = null){
        unset($variables);
        $this->action_handler = new Subcategorymanager_Model_Subcategorymanager;
        return $this->action_handler->main($variables);
    }
    
    private function get_other_data(array $variables = null){
        $this->get_access_levels_list = new Privileges_Data_Getpriviligeslist;
        $this->get_statuses_list = new Accountsstatus_Data_Getaccountsstatus;
        $this->get_category_lists = new Subcategorymanager_Data_Getallcategories;
        $this->get_images_list = new Modulemanager_Data_Selectimage;
        $this->core_controller_csrftoken = new Core_Controller_Csrftoken;
        
        $page['access_level'] = $this->get_access_levels_list->main($variables);
        $page['statuses_name'] = $this->get_statuses_list->main($variables);
        $page['category_lists'] = $this->get_category_lists->main($variables);
        $page['image_list'] = $this->get_images_list->main($variables);
        $page['csrf_token'] = $this->core_controller_csrftoken->main($variables);
        
        return $page;
    }

}
